<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：团队控制器。
 *
 **/
namespace Api\Controller;

class TeamController extends ComController
{
    /**
     * 团队等级页面
     */
    public function index()
    {
        //当前已推广会员数
        $countSql = "select count(*) as total_num from dz_user a where a.pid='{$this->user['id']}' and a.id in (select b.uid from dz_user_level_buy b where b.`status`=2 group by b.uid)";
        $countQuery = M()->query($countSql);
        $tg_num = $countQuery[0]['total_num'];
        $data['head_img'] = $this->user['head_img'];
        $data['int_code'] = $this->user['int_code'];
        if(!$this->user['team_level']){
            $data['team_level'] = "0";
            //下一个会员等级
            $team_next_level = M('team_level')->where(array('o'=>1))->find();
            $data['team_next_level'] = $team_next_level['o'];
            //计算已完成百分比
            $data['percent'] = round($tg_num / $team_next_level['min_num'] * 100);
        }else{
            //下一个会员等级
            $team_level = M('team_level')->where(array('name'=>$this->user['team_level']))->find();
            $data['team_level'] = $team_level['o'];
            $team_next_level = M('team_level')->where(array('o'=>array('gt',$team_level['o'])))->limit(1)->order('o asc')->select();
            //计算已完成百分比
            if(empty($team_next_level)){
                $data['team_next_level'] = $team_level['o'];
                $data['percent'] = "100";
            }else{
                $next_num = $team_next_level[0]['min_num'];
                $data['team_next_level'] = $team_next_level[0]['o'];
                $data['percent'] = round($tg_num / $team_next_level[0]['min_num'] * 100);
            }
        }
        //团队等级项目说明
        $team_level_list = M('team_level')->order('o asc')->select();
        foreach ($team_level_list as $k=>&$v){
            $user_level = M('user_level')->where('id='.$v['user_level_id'])->find();
            $team_level_list[$k]['user_level_name'] = $user_level['title'];
        }
        $this->res['data'] = $data;
        $this->res['data']['team_level_list'] = $team_level_list;
        $this->response($this->res,'json');
    }
    
    /**
     * 团队人数
     */
    public function teamPersons()
    {
		$k = "HONGBAO_XJ";
		$var = M('setting')->where("k='$k'")->find();
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $model = M();
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        
        $sqlWhere = "1=1 and ";
        if($this->user['id']){
            $sqlWhere .= "a.pid='{$this->user['id']}' and ";
        }
        $sqlWhere = msubstr($sqlWhere, 0, utf8_strlen($sqlWhere) - 4, "utf-8", false);
        $count = $model->query("select count(*) as total_num from ".C('DB_PREFIX')."user a where ".$sqlWhere);
        $count = $count[0]['total_num'];
        $sql = "select a.id,a.username,a.t,a.level_id from ".C('DB_PREFIX')."user a where ".$sqlWhere." order by a.t desc limit ".$offset . "," . $pagesize;
        $list = $model->query($sql);
        foreach ($list as $k=>&$v){
            $user_level = M('user_level')->where('id='.$v['level_id'])->find();
            $list[$k]['level_name'] = $user_level['title'];
            $list[$k]['t'] = date('Y-m-d H:i:s',$v['t']);
            $list[$k]['username'] = yc_phone($v['username']); 
			$list[$k]['money_in'] = $var['v'];
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        // 有效推广人数
        $mcount = $model->query("select count(*) as total_num from ".C('DB_PREFIX')."user a where a.pid='{$this->user['id']}' and a.id in (select b.uid from ".C('DB_PREFIX')."user_level_buy b where b.`status`=2 group by b.uid)");
        $mcount = $mcount[0]['total_num'];
        $this->res['data']['tg_num'] = $mcount;
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
   
    /**
     * 推荐奖励
     */
    public function tuijianReward()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $m = M('user_income');
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $where['uid'] = $this->user['id'];
        $where['type'] = array('in','4,5');
        $count = $m->where($where)->count();
        $list = $m->order('t desc')->where($where)->field('id,money,t,type,remark')->limit($offset . ',' . $pagesize)->select();
        
        $total_sum = $m->where($where)->sum('money');
        if(!$total_sum){
            $total_sum = 0;
        }
        
        foreach ($list as $key=>&$val){
            $list[$key]['t'] = date('Y-m-d H:i:s',$val['t']);
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->res['data']['total_price'] = $total_sum;
        $this->response($this->res,'json');
    }
}